﻿var news =
{
	isInitialized: false,
	$container: null,
	numEntries: 0,

	init: function ()
	{
		news.$container = $("#news");

		news.numEntries = GadgetPrefs.getInt("NewsNumEntries");

		news.isInitialized = true;
	},

	display: function ()
	{
		if (!news.isInitialized)
		{
			news.init();
		}

		if (news.numEntries > 0)
		{
			news.load();
		}
		else
		{
			news.$container.hide();
			gadgets.window.adjustHeight();
		}
	},

	load: function ()
	{
		var params = {};
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.FEED;
		params[gadgets.io.RequestParameters.NUM_ENTRIES] = news.numEntries;
		params[gadgets.io.RequestParameters.GET_SUMMARIES] = true;

		gadgets.io.makeRequest("http://news.gismeteo.ru/export/gismeteo" + news.numEntries + ".xml", news.render, params);
	},

	render: function (response)
	{
		var content = "";

		if (response && response.data)
		{
			var feed = response.data;

			for (var i in feed.Entry)
			{
				content += "<div class=\"item\">";
				content += "<div class=\"title\"><a target=\"_blank\" " + "href=\"" + feed.Entry[i].Link + "\">" + feed.Entry[i].Title + "</a></div>";

				var date = new Date(feed.Entry[i].Date);
				content += "<div class=\"date\">" + date.toLocaleDateString() + " " + date.toLocaleTimeString() + "</div>";

				content += "<div class=\"summary\">" + feed.Entry[i].Summary + "</div>";
				content += "</div>";
			}
		}
		else
		{
			content = GadgetPrefs.getMsg("News_Error");
		}

		news.$container.find(".content").html(content);
		news.$container.show();
		gadgets.window.adjustHeight();
	}
};

var forecast =
{
	isInitialized: false,
	$container: null,
	regionCode: 0,
	countryCode: 0,
	areaCode: 0,
	cityCode: 0,
	cityIndex: 0,
	degree: "C",

	init: function ()
	{
		forecast.$container = $("#forecast");

		forecast.regionCode = GadgetPrefs.getInt("Region");
		forecast.countryCode = GadgetPrefs.getInt("Country");
		forecast.areaCode = GadgetPrefs.getInt("Area");
		forecast.cityCode = GadgetPrefs.getInt("CityCode");
		forecast.cityIndex = GadgetPrefs.getInt("CityIndex");
		forecast.degree = GadgetPrefs.getString("Degree");

		forecast.isInitialized = true;
	},

	display: function ()
	{
		if (!forecast.isInitialized) forecast.init();

		if (forecast.regionCode < 1 || forecast.countryCode < 1 || forecast.areaCode == 0 || forecast.cityIndex < 1)
		{
			forecast.$container.hide();
			preferences.display();
		}
		else
		{
			forecast.load();
		}
	},

	load: function ()
	{
		var params = {};
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
		params[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 600;

		gadgets.io.makeRequest("http://informer.gismeteo.ru/xml/" + GadgetPrefs.getInt("CityIndex") + "_1.xml", forecast.render, params);
	},

	render: function (response)
	{
		var forecastTitle = GadgetPrefs.getMsg("Forecast_Header");
		var content = GadgetPrefs.getMsg("Forecast_Error");

		if (response && response.data)
		{
			forecastTitle = "<a target=\"_blank\" href=\"http://www.gismeteo.ru/city/hourly/" + GadgetPrefs.getInt("CityCode") + "/\">" + decode(response.data.getElementsByTagName("TOWN")[0].getAttribute("sname")) + "</a>";

			content = "<table id=\"items\" cellpadding=\"2\" cellspacing=\"0\">";
			content += "<tr><td class=\"header\"></td>";
			content += "<td class=\"header\">" + GadgetPrefs.getMsg("Header_Precipitations") + "</td>";
			content += "<td class=\"header\">" + GadgetPrefs.getMsg("Header_Temperature") + forecast.degree + "</td>";
			content += "<td class=\"header\">" + GadgetPrefs.getMsg("Header_Wind") + "</td>";
			content += "<td class=\"header\">" + GadgetPrefs.getMsg("Header_Pressure") + "</td>";
			content += "</tr>";

			var nodes = response.data.getElementsByTagName("FORECAST");
			var node = null;
			var item = null;

			for (var i = 0; i < nodes.length; i++)
			{
				node = nodes.item(i);

				content += "<tr>";

				//forecast info
				var fi = {};
				fi.day = parseInt(node.getAttribute("day"), 10);
				fi.month = parseInt(node.getAttribute("month"), 10);
				fi.year = parseInt(node.getAttribute("year"), 10);
				fi.TOD = {};
				fi.TOD.index = parseInt(node.getAttribute("tod"), 10);
				fi.TOD.title = GadgetPrefs.getMsg("TOD_" + fi.TOD.index);
				fi.weekday = {};
				fi.weekday.index = parseInt(node.getAttribute("weekday"), 10);
				fi.weekday.title = GadgetPrefs.getMsg("weekday_" + fi.weekday.index);

				var dateFormat = GadgetPrefs.getMsg("DateFormat");
				var dateString = dateFormat.replace("dd", firstZero(fi.day)).replace("MM", firstZero(fi.month)).replace("yyyy", firstZero(fi.year));

				content += "<td>";
				content += "<div>" + fi.weekday.title + ", " + fi.TOD.title + "</div>";
				content += "<div class=\"date\">" + dateString + "</div>";
				content += "</td>";

				//Phenomena info (image)
				item = node.getElementsByTagName("PHENOMENA")[0];
				var phi = {};
				phi.cloudiness = parseInt(item.getAttribute("cloudiness"), 10);
				phi.precipitation = {};
				phi.precipitation.codes = { "4": "r3", "5": "r4", "6": "s3", "7": "s4" };
				phi.precipitation.type = parseInt(item.getAttribute("precipitation"), 10);
				phi.precipitation.code = phi.precipitation.codes[phi.precipitation.type];
				phi.rPower = parseInt(item.getAttribute("rpower"), 10);
				phi.sPower = parseInt(item.getAttribute("spower"), 10);

				var title = GadgetPrefs.getMsg("phenomena_" + phi.cloudiness);
				var pt = GadgetPrefs.getMsg("phenomena_" + phi.precipitation.type);
				if (pt)
				{
					title += " " + pt;
				}

				content += "<td class=\"phenomena\" title=\"" + title + "\">";
				content += "<img src=\"http://i.gismeteo.com/static/images/icons/new/" +
					(fi.TOD.index == 0 || fi.TOD.index == 3 ? "n.moon" : "d.sun") +
					(phi.cloudiness > 0 ? ".c" + phi.cloudiness : "") +
					((phi.precipitation.code) ? ("." + phi.precipitation.code) : "") +
					((phi.sPower > 0 || phi.precipitation.type == 8) ? ".st" : "") + ".png\"  alt=\"\" />";
				content += "</td>";

				//Temperature info
				item = node.getElementsByTagName("TEMPERATURE")[0];
				var ti = {};
				ti.min = parseInt(item.getAttribute("min"), 10);
				ti.max = parseInt(item.getAttribute("max"), 10);

				if (forecast.degree == "F")
				{
					ti.min = ti.min * (9 / 5) + 32;
					ti.max = ti.max * (9 / 5) + 32;
				}

				ti.avg = Math.round((ti.min + ti.max) / 2);

				content += "<td class=\"temperature\" title=\"" + firstSign(ti.min) + ".." + firstSign(ti.max) + "\">";
				content += firstSign(ti.avg) + "°";
				content += "</td>";

				//Wind info
				item = node.getElementsByTagName("WIND")[0];
				var wi = {};
				wi.speed = {};
				wi.speed.min = parseInt(item.getAttribute("min"), 10);
				if (wi.speed.min < 0) wi.speed.min = 0;
				wi.speed.max = parseInt(item.getAttribute("max"), 10);
				if (wi.speed.max < 0) wi.speed.max = 0;
				wi.speed.avg = Math.round((wi.speed.min + wi.speed.max) / 2);
				wi.direction = {};
				wi.direction.index = parseInt(item.getAttribute("direction"), 10);
				wi.direction.title = GadgetPrefs.getMsg("wind_" + wi.direction.index);

				content += "<td class=\"wind\" title=\"" + wi.direction.title + ", " + wi.speed.min + ".." + wi.speed.max + "\">";
				content += "<table><tr><td><div class=\"wind wind" + (wi.speed.avg > 0 ? (" wind" + wi.direction.index) : "") + "\"></div></td><td>" + (wi.speed.avg > 0 ? wi.speed.avg : "") + "</td></tr></table>";
				content += "</td>";

				//Pressure info
				item = node.getElementsByTagName("PRESSURE")[0];
				var pi = {};
				pi.min = parseInt(item.getAttribute("min"), 10);
				pi.max = parseInt(item.getAttribute("max"), 10);
				pi.avg = Math.round((pi.min + pi.max) / 2);

				content += "<td class=\"pressure\" title=\"" + pi.min + ".." + pi.max + "\">";
				content += pi.avg;
				content += "</td>";

				content += "</tr>";
			}

			content += "</table>";
		}

		forecast.$container.find(".header .title").html(forecastTitle);
		forecast.$container.find(".content").html(content);
		forecast.$container.show();

		gadgets.window.adjustHeight();

		//setTimeout(gplusone.show, 1000);
	}
};

var gplusone =
{
	show: function ()
	{
		
		gplusone.hide();
		$("#gplusone").append($("<div/>").addClass("g-plusone").attr({ "data-size": "medium", "data-annotation": "none", "data-href": "http://www.google.com/ig/adde?moduleurl=kxmepuk.googlecode.com/hg/gadget/gismeteo/weather-forecast/gadget.xml" }));
		
		var script = document.createElement("script");
		script.id = "g-plusone-script";
		script.type = "text/javascript";
		script.async = true;
		script.src = gplusone.getSrc();

		var head = document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0];
		if (head)
		{
			head.insertBefore(script, head.firstChild);
		}
		
	},

	hide: function ()
	{
		$("#gplusone").empty();
		$("script[src*='plusone.js']").remove();
		$("iframe").remove();
	},

	getSrc: function ()
	{
		return ((document.location.protocol == "https:" ? "https://" : "http://") + "apis.google.com/js/plusone.js");
	}

};

var preferences =
{
	isInitialized: false,

	$container: null,

	$region: null,
	$country: null,
	$area: null,
	$city: null,
	$degree: null,
	$newsNumEntries: null,

	$save: null,
	$cancel: null,

	init: function ()
	{
		if (!preferences.isInitialized)
		{
			preferences.$container = $("#preferences");
			preferences.$region = preferences.$container.find("#preferences-region");
			preferences.$country = preferences.$container.find("#preferences-country");
			preferences.$area = preferences.$container.find("#preferences-area");
			preferences.$city = preferences.$container.find("#preferences-city");
			preferences.$degree = preferences.$container.find("#preferences-degree");
			preferences.$newsNumEntries = preferences.$container.find("#preferences-news-num-entries");
			preferences.$save = preferences.$container.find("#preferences-save");
			preferences.$cancel = preferences.$container.find("#preferences-cancel");

			preferences.$region.change(preferences.region.change);
			preferences.$country.change(preferences.country.change);
			preferences.$area.change(preferences.area.change);
			preferences.$city.change(preferences.city.change);
			preferences.$degree.change(preferences.degree.change);
			preferences.$newsNumEntries.change(preferences.newsNumEntries.change);

			preferences.$save.click(preferences.save);
			preferences.$cancel.click(preferences.cancel);

			preferences.isInitialized = true;
		}
	},

	display: function ()
	{
		if (!preferences.isInitialized) preferences.init();
		if (!news.isInitialized) news.init();

		forecast.$container.hide();
		preferences.$container.show();

		preferences.degree.display();
		preferences.newsNumEntries.display();
		preferences.region.display();

		gadgets.window.adjustHeight();
	},

	save: function ()
	{
		GadgetPrefs.set("Region", forecast.regionCode);
		GadgetPrefs.set("Country", forecast.countryCode);
		GadgetPrefs.set("Area", forecast.areaCode);
		GadgetPrefs.set("CityCode", forecast.cityCode);
		GadgetPrefs.set("CityIndex", forecast.cityIndex);
		GadgetPrefs.set("Degree", forecast.degree);
		GadgetPrefs.set("NewsNumEntries", news.numEntries);

		preferences.cancel(null);
	},

	cancel: function (e)
	{
		if (e)
		{
			forecast.regionCode = GadgetPrefs.getInt("Region");
			forecast.countryCode = GadgetPrefs.getInt("Country");
			forecast.areaCode = GadgetPrefs.getInt("Area");
			forecast.cityCode = GadgetPrefs.getInt("CityCode");
			forecast.cityIndex = GadgetPrefs.getInt("CityIndex");
			forecast.degree = GadgetPrefs.getString("Degree");
			news.numEntries = GadgetPrefs.getInt("NewsNumEntries");
		}

		preferences.$container.hide();
		news.display();
		forecast.display();
	},

	setEnable: function ($object, enabled)
	{
		if ($object)
		{
			if (enabled)
			{
				$object.removeAttr("disabled");
			}
			else
			{
				$object.attr({ "disabled": "disabled" });
			}
		}
	}
};

preferences.cache = {};

preferences.data =
{
	load: function (section, parameters, callback)
	{
		var postData = gadgets.io.encodeValues(parameters);
		var cacheKey = section + "?" + postData;

		if (preferences.cache[cacheKey])
		{
			//get from cache
			if (callback)
			{
				callback(preferences.cache[cacheKey]);
			}
		}
		else
		{
			var params = {};
			params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
			params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
			params[gadgets.io.RequestParameters.POST_DATA] = postData;

			gadgets.io.makeRequest("http://informer.gismeteo.ru/getcode/index.php", function (response) { preferences.data.onLoad(section, parameters, callback, response); }, params);
		}
	},

	onLoad: function (section, parameters, callback, response)
	{
		var items = null;

		if (response && response.data)
		{
			items = new Array();

			var root = response.data.getElementsByTagName(section)[0];

			if (root.hasChildNodes())
			{
				//get nodes
				for (var i = 0; i < root.childNodes.length; i++)
				{
					var node = root.childNodes[i];
					var item = { text: node.text || node.textContent };

					//get attributes
					if (node.attributes && node.attributes.length > 0)
					{
						item.attributes = {};

						for (var j = 0; j < node.attributes.length; j++)
						{
							item.attributes[node.attributes[j].name] = node.attributes[j].value;
						}
					}

					items[i] = item;
				}
			}

			//store to cache
			preferences.cache[section + "?" + gadgets.io.encodeValues(parameters)] = items;
		}

		if (callback)
		{
			callback(items);
		}
	}
};

preferences.degree =
{
	display: function ()
	{
		setTimeout(preferences.degree.locate, 1);
	},

	locate: function ()
	{
		preferences.$degree.find("[value='" + forecast.degree + "']").attr("selected", "selected");
		preferences.degree.change();
	},

	change: function ()
	{
		forecast.degree = preferences.$degree.find("option:selected").val();
	}
};

preferences.newsNumEntries =
{
	display: function ()
	{
		setTimeout(preferences.newsNumEntries.locate, 1);
	},

	locate: function ()
	{
		preferences.$newsNumEntries.find("[value='" + news.numEntries + "']").attr("selected", "selected");
		preferences.newsNumEntries.change();
	},

	change: function ()
	{
		news.numEntries = preferences.$newsNumEntries.find("option:selected").val();
	}
};

preferences.region =
{
	display: function ()
	{
		setTimeout(preferences.region.locate, 1);
	},

	locate: function ()
	{
		preferences.$region.find("[value='" + forecast.regionCode + "']").attr("selected", "selected");
		preferences.region.change();
	},

	change: function ()
	{
		forecast.regionCode = preferences.$region.find("option:selected").val();

		var noData = GadgetPrefs.getMsg("Preferences_NoData");
		preferences.$country.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-1).text(noData));
		preferences.$area.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-1).text(noData));
		preferences.$city.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-1).text(noData));

		preferences.setEnable(preferences.$save, (preferences.$city.find("option:selected").val() > 0));

		preferences.country.display();
	}
};

preferences.country =
{
	display: function ()
	{
		if (forecast.regionCode > 0)
		{
			preferences.$country.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-2).text(GadgetPrefs.getMsg("Preferences_Loading")));
			preferences.data.load("countries", { "a0": forecast.regionCode }, preferences.country.render);
		}
	},

	render: function (items)
	{
		preferences.$country.empty();

		if (items && items.length > 0)
		{
			preferences.$country.removeAttr("disabled").append($("<option/>").val(0).text(GadgetPrefs.getMsg("Preferences_NotSelected")));

			for (var i = 0; i < items.length; i++)
			{
				preferences.$country.append($("<option/>").attr(items[i].attributes).text(items[i].text));
			}
		}
		else
		{
			preferences.$country.attr({ "disabled": "disabled" }).append($("<option/>").val(-2).text(GadgetPrefs.getMsg("Preferences_Error")));
		}

		setTimeout(preferences.country.locate, 1);
	},

	locate: function ()
	{
		preferences.$country.find("[value='" + forecast.countryCode + "']").attr("selected", "selected");
		preferences.country.change();
	},

	change: function ()
	{
		forecast.countryCode = preferences.$country.find("option:selected").val();

		var noData = GadgetPrefs.getMsg("Preferences_NoData");
		preferences.$area.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-1).text(noData));
		preferences.$city.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-1).text(noData));

		preferences.setEnable(preferences.$save, (preferences.$city.find("option:selected").val() > 0));

		preferences.area.display();
	}
};

preferences.area =
{
	display: function ()
	{
		if (forecast.countryCode > 0)
		{
			preferences.$area.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-2).text(GadgetPrefs.getMsg("Preferences_Loading")));
			preferences.data.load("districts", { "a1": forecast.countryCode }, preferences.area.render);
		}
	},

	render: function (items)
	{
		preferences.$area.empty();

		if (items)
		{
			if (items.length > 0)
			{
				preferences.$area.removeAttr("disabled").append($("<option/>").val(0).text(GadgetPrefs.getMsg("Preferences_NotSelected")));

				for (var i = 0; i < items.length; i++)
				{
					preferences.$area.append($("<option/>").attr(items[i].attributes).text(items[i].text));
				}
			}
			else
			{
				preferences.$area.attr({ "disabled": "disabled" }).append($("<option/>").val(-1).text(GadgetPrefs.getMsg("Preferences_NoData")));
			}
		}
		else
		{
			preferences.$area.attr({ "disabled": "disabled" }).append($("<option/>").val(-2).text(GadgetPrefs.getMsg("Preferences_Error")));
		}

		setTimeout(preferences.area.locate, 1);
	},

	locate: function ()
	{
		preferences.$area.find("[value='" + forecast.areaCode + "']").attr("selected", "selected");
		preferences.area.change();
	},

	change: function ()
	{
		forecast.areaCode = preferences.$area.find("option:selected").val();

		preferences.$city.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-1).text(GadgetPrefs.getMsg("Preferences_NoData")));

		preferences.setEnable(preferences.$save, (preferences.$city.find("option:selected").val() > 0));

		preferences.city.display();
	}
};

preferences.city =
{
	display: function ()
	{
		if (forecast.areaCode > 0)
		{
			preferences.$city.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-2).text(GadgetPrefs.getMsg("Preferences_Loading")));
			preferences.data.load("cities", { "a2": forecast.areaCode }, preferences.city.render);
		}
		else if (forecast.areaCode == -1 && forecast.countryCode > 0)
		{
			preferences.$city.empty().attr({ "disabled": "disabled" }).append($("<option/>").val(-2).text(GadgetPrefs.getMsg("Preferences_Loading")));
			preferences.data.load("cities", { "a2n": forecast.countryCode }, preferences.city.render);
		}
	},

	render: function (items)
	{
		preferences.$city.empty();

		if (items && items.length > 0)
		{
			preferences.$city.removeAttr("disabled").append($("<option/>").val(0).text(GadgetPrefs.getMsg("Preferences_NotSelected")));

			for (var i = 0; i < items.length; i++)
			{
				preferences.$city.append($("<option/>").attr(items[i].attributes).text(items[i].text));
			}
		}
		else
		{
			preferences.$city.attr({ "disabled": "disabled" }).append($("<option/>").val(-2).text(GadgetPrefs.getMsg("Preferences_Error")));
		}

		setTimeout(preferences.city.locate, 1);
	},

	locate: function ()
	{
		preferences.$city.find("[value='" + forecast.cityCode + "']").attr("selected", "selected");
		preferences.city.change();
	},

	change: function ()
	{
		var $selected = preferences.$city.find("option:selected");
		forecast.cityCode = $selected.val();
		forecast.cityIndex = $selected.attr("old_id") || 0;

		preferences.setEnable(preferences.$save, (preferences.$city.find("option:selected").val() > 0));
	}
};

var analytics =
{
	init: function ()
	{
		_gaq = window._gaq || [];
		_gaq.push(['_setAccount', GadgetPrefs.getString("GoogleAnalyticsCode")]);
		_gaq.push(['_trackPageview']);

		var script = document.createElement("script");
		script.type = "text/javascript";
		script.async = true;
		script.src = (document.location.protocol == "https:" ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";

		var head = document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0];

		if (head)
		{
			head.insertBefore(script, head.firstChild);
		}
	}
};

function decode(code)
{
	code = code.replace(/\+/g, ' ').replace(/%21/g, '!').replace(/%22/g, '"').replace(/%23/g, '#').replace(/%24/g, '$').replace(/%2D/g, '-').replace(/%5E/g, '^').replace(/%26/g, '&').replace(/%B9/g, '№').replace(/%3B/g, ';').replace(/%25/g, '%').replace(/%3A/g, ':').replace(/%3F/g, '?').replace(/%28/g, '(').replace(/%29/g, ')').replace(/%BF/g, 'ї').replace(/%AF/g, 'Ї').replace(/%B3/g, 'і').replace(/%B2/g, 'І').replace(/%BA/g, 'є').replace(/%AA/g, 'Є');
	code = code.replace(/%C0/g, 'А').replace(/%C1/g, 'Б').replace(/%C2/g, 'В').replace(/%C3/g, 'Г').replace(/%C4/g, 'Д').replace(/%C5/g, 'Е').replace(/%A8/g, 'Ё').replace(/%C6/g, 'Ж').replace(/%C7/g, 'З').replace(/%C8/g, 'И').replace(/%C9/g, 'Й').replace(/%CA/g, 'К').replace(/%CB/g, 'Л').replace(/%CC/g, 'М').replace(/%CD/g, 'Н').replace(/%CE/g, 'О').replace(/%CF/g, 'П').replace(/%D0/g, 'Р').replace(/%D1/g, 'С').replace(/%D2/g, 'Т').replace(/%D3/g, 'У').replace(/%D4/g, 'Ф').replace(/%D5/g, 'Х').replace(/%D6/g, 'Ц').replace(/%D7/g, 'Ч').replace(/%D8/g, 'Ш').replace(/%D9/g, 'Щ').replace(/%DA/g, 'Ъ').replace(/%DB/g, 'Ы').replace(/%DC/g, 'Ь').replace(/%DD/g, 'Э').replace(/%DE/g, 'Ю').replace(/%DF/g, 'Я');
	code = code.replace(/%E0/g, 'а').replace(/%E1/g, 'б').replace(/%E2/g, 'в').replace(/%E3/g, 'г').replace(/%E4/g, 'д').replace(/%E5/g, 'е').replace(/%E6/g, 'ж').replace(/%B8/g, 'ё').replace(/%E7/g, 'з').replace(/%E8/g, 'и').replace(/%E9/g, 'й').replace(/%EA/g, 'к').replace(/%EB/g, 'л').replace(/%EC/g, 'м').replace(/%ED/g, 'н').replace(/%EE/g, 'о').replace(/%EF/g, 'п').replace(/%F0/g, 'р').replace(/%F1/g, 'с').replace(/%F2/g, 'т').replace(/%F3/g, 'у').replace(/%F4/g, 'ф').replace(/%F5/g, 'х').replace(/%F6/g, 'ц').replace(/%F7/g, 'ч').replace(/%F8/g, 'ш').replace(/%F9/g, 'щ').replace(/%FA/g, 'ъ').replace(/%FB/g, 'ы').replace(/%FC/g, 'ь').replace(/%FD/g, 'э').replace(/%FE/g, 'ю').replace(/%FF/g, 'я');
	return (code);
}

function firstZero(value)
{
	var result = value.toString();

	return ((result.length > 1 ? "" : "0") + result);
}

function firstSign(value)
{
	return ((value > 0 ? "+" : "") + value);
}
